#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)


int partition(int arr[], int left, int right) {
    int pivot = arr[left];

    int i = left, j = right;
    while (i < j) {
        while (i < j && arr[j] >= pivot) {
            j--;
        } // i == j || arr[j] < pivot 
        arr[i] = arr[j];

        while (i < j && arr[i] <= pivot) {
            i++;
        } // i == j || arr[i] > pivot
        arr[j] = arr[i];
    } // i == j

    arr[i] = pivot;
    return i;
}

int find_kth_minimun(int arr[], int n, int k) {
    k--;
    int left = 0, right = n - 1;
    while (left < right) {
        int idx = partition(arr, left, right);

        if (idx < k) {
            left = idx + 1;
        }
        else if (idx > k) {
            right = idx - 1;
        }
        else {
            return arr[idx];
        }
    } // left == right

    return arr[left];
}
